//
//  LYCLoginViewController.swift
//  新浪微博
//
//  Created by liuyanchi on 16/7/29.
//  Copyright © 2016年 liuyanchi. All rights reserved.
//

import UIKit

import SVProgressHUD

class LYCLoginViewController: UIViewController,UIWebViewDelegate {
    
    // MARK:-- 设置webView
    let webView = UIWebView()
    
    
    override func loadView() {
        
        view = webView
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.whiteColor()
        
        setupNav()
        
        // 1.设置网络请求
        let url = "https://api.weibo.com/oauth2/authorize?client_id=385705346&redirect_uri=http://www.itheima.com"
        
//        let url = "https://api.weibo.com/oauth2/authorize?client_id=1406121621&redirect_uri=http://www.itheima.cn&language=en"

        
        // 2.创建请求
        let request = NSURLRequest(URL: NSURL(string: url)!)
        
        // 3.网页的加载
        webView.loadRequest(request)
        
        // 4.设置代理
        webView.delegate = self
        
    }
    
    //MARK:--item的设置
    private func setupNav(){
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: UIBarButtonItemStyle.Plain, target: self, action: "auto")
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.Plain, target: self, action: "dissmiss")
        
        
    }
    
    //MARK:--按钮的点击方法
    @objc private func auto(){
        
        // 账号密码的自动填充
        let js = "document.getElementById('userId').value = '18266187011';document.getElementById('passwd').value = 'liuyanchi110'"
        
//         let js = "document.getElementById('userId').value = 'itcast123456@hotmail.com';document.getElementById('passwd').value = 'itcast123456'"
        
        
        webView.stringByEvaluatingJavaScriptFromString(js)
        
    }
    
    @objc private func dissmiss(){
        
        dismissViewControllerAnimated(false) { () -> Void in
            
        }
        
    }
    
}

// MARK:--webView的代理方法

extension LYCLoginViewController{
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        if let urlString = request.URL?.absoluteString {
            
            if urlString.hasPrefix(RedrectURL){
                
                if let code = request.URL?.query{
                    
                    let subStr = code.substringFromIndex("code=".endIndex)
                 
                    // 与viewModel建立联系
                    LYCLoginViewModel.shareInstance.loadToken(subStr, success: { () -> () in
                        
                        // 成功的处理
                        printLog("成功的建立回调")
                        
                         // MARK:--页面的消失
                        self.dissmiss()
                        
                        SVProgressHUD.dismiss()
                        
                        }, failed: { (message) -> () in
                            
                            // 失败的处理
                            SVProgressHUD.showErrorWithStatus(message)
                    })
                    
//                    取消回调网页?
                    return false
                }
                
            }
            
        }
        
        // 返回false,关闭回调页面
        return true
    }
    
    // web刚刚开始加载的时候
    func webViewDidStartLoad(webView: UIWebView) {
        
        // 显示一个加载忙的图标
        SVProgressHUD.show()
        
    }
    
    // web页面加载完成的时候
    func webViewDidFinishLoad(webView: UIWebView) {
        
        
        SVProgressHUD.dismiss()
    }
    
    // web页面加载错误的时候
    func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
        
        SVProgressHUD.dismiss()
        
    }
    
}

// MARK:--利用code获取token
extension LYCLoginViewController{
    
    
}

// MARK:--测试token,通过token加载个人信息
extension LYCLoginViewController{
    
    
}